﻿using System;
using System.Text.RegularExpressions;

namespace testy {
    public class RegexHelper {
        public static string GetFirstCapture(string inputString, string pattern) {
            var regex = new Regex(pattern, RegexOptions.Multiline);
            MatchCollection matchCollection = regex.Matches(inputString);
            foreach (Match match in matchCollection) {
                return match.Value;
            }
            return "";
        }

        public static string GetFirstMatch(string inputString, string pattern) {
            var regex = new Regex(pattern, RegexOptions.Multiline);
            MatchCollection matchCollection = regex.Matches(inputString);
            foreach (Match match in matchCollection) {
                return match.Groups[1].Value;
            }
            return "";
        }

        public static void WalkFormats(string parameter, RegexTest.FormatPrinter formatPrinter) {
            string[] strings = parameter.Split(',');
            foreach (string s in strings) {
                string[] formats = s.Split('|');
                int number = Int32.Parse(formats[0]);
                string url = formats[1];
                url = url.Replace(@"\/", @"/").Replace(@"\u0026", @"&");
                var uri = new Uri(url);
                formatPrinter(number, uri.OriginalString);
            }
        }
    }
}